﻿using MediatR;
using SmartCity.ProcessEngine.API.Read.Application.Commands.Forms;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Extensions;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.Forms
{
    /// <summary>
    /// 表单级联命令
    /// </summary>
    public class FormCascaderCommandHandle:IRequestHandler<FormCascaderCommand,ApiResult<List<FormDto>>>
    {
        private readonly IBaseRepository<WF_Form> formRep;

        public FormCascaderCommandHandle(IBaseRepository<WF_Form> formRep)
        {
            this.formRep = formRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回级联</returns>
        public Task<ApiResult<List<FormDto>>> Handle(FormCascaderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<FormDto>> result = new ApiResult<List<FormDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = ApiEnum.SearchSuccess.GetDescription();

            var list = from a in formRep.GetAll()
                       where !a.IsDeleted && a.TypeNo == request.TypeNo
                       select new FormDto
                       {
                           FormNo = a.FormNo,
                           FormName = a.FormName,
                       };
            result.data = list.ToList();

            return Task.FromResult(result);
               
        }
    }
}
